<?
	include('common.php');
	include('config.php');
?>
<?php
    class db
    {
       	$conn=NULL;
        public $result=NULL;
        private $author="K H A";
        protected $version="1.1";
        function __construct()
        {
        	if ($this->conn==NULL) {
	            $this->conn=mysql_connect($this->host,$this->user,$this->pass);
	            mysql_select_db($this->database,$this->conn);
	            mysql_query("set names 'utf8'");
        	}
        }
		function get_by_query($query){
			$kq=mysql_query($query) or die(mysql_error());
			return $kq;
		}
		/** get_by_order là hàm dùng để lấy table cho tùy biến sắp xếp **/
		function get_by_order($table,$id=-1,$displayorder=-1,$dk1='') {
			settype($id, "int");
			settype($anhien, "int");
			$query = "SELECT * FROM ".$table." WHERE (displayorder='$displayorder' or '$displayorder'=-1) and (id='$id' or '$id'=-1) ".$dk1;
                     //   echo $query;
			$kq=mysql_query($query) or die(mysql_error());
			return $kq;
		}
		function get_thutu($table) {
			$sql="select thutu from $table order by thutu desc limit 0,1";
			$kq=mysql_query($sql) or die(mysql_error()); 
			$row=mysql_fetch_assoc($kq);
			return ($row['thutu']+1);		
		}
		/** anhien là hàm dùng để lấy thay đổi giá trị bool 1 feild nào đó kết quả trả về là giá trị đã thay đổi **/
		function anhien($table,$id,$loai)
		{
			$query="select ".$loai." from ".$table." where id=$id";
			$rs = mysql_query($query) or die(mysql_error());
			$row_rs = mysql_fetch_row($rs);
			$anhien = $row_rs[0];
			if($anhien==1) $anhien=0; else $anhien=1;
			$query = "UPDATE ".$table." SET ".$loai." = $anhien where id=$id";
			mysql_query($query) or die(mysql_error()); 
			return $anhien;		
		}
		function insertObject($table,$array){
			foreach($array as $giatri1 => $giatri2)
			{
				$field=$field.','.$giatri1;
				$value=$value.','.format($giatri2,0);
			}
			$field=substr($field,1);
			$value=substr($value,1);
			$str = "insert into $table ($field) values ($value)";
			//echo $str;die;
			$res = mysql_query($str) or die(mysql_error());
			if($res){
				return $res;
			}else return "<h1 style='color:#FF0000'>"."InsertObject Error!"."</h1>";
		}
		function updateObject($table,$array,$where){
		foreach($array as $giatri1 => $giatri2)
		{
			$value=$value.','.$giatri1.'='.format($giatri2,0);
		}
		$value=substr($value,1);
		$str = "update $table set $value where $where";
		//echo $str;die;
		$res=mysql_query($str);
		$res = mysql_query($str) or die(mysql_error());
			if($res){
				return $res;
			}else return "<h1 style='color:#FF0000'>"."InsertObject Error!"."</h1>";
		}
		
		/** get_phantrang_db là hàm phân trang theo thứ tự pagenum là trang hiện tại, pagesize là muốn lấy bao nhiêu record **/
		function get_phantrang_db($pageNum=1,$pageSize=9,&$totalRows,$table,$id=-1,$status=-1,$dk1='',$dk2='') {
			settype($id, "int");
			settype($anhien, "int");
			settype($pageNum, "int");
			if($pageNum<=0) $pageNum=1;
			 $totalRows = 0;
			$query = "SELECT count(id) FROM ".$table." WHERE (status='$status' or '$status'=-1) and (id='$id' or '$id'=-1) ".$dk1." order by displayorder desc,id desc ".$dk2;
			//echo $query;die;
			$kq = mysql_query($query) or die (mysql_error());
            $row_kq = mysql_fetch_row($kq);
            $totalRows = $row_kq[0];

            $startRow = ($pageNum - 1)*$pageSize;
			$query = "SELECT * FROM ".$table." WHERE (status='$status' or '$status'=-1) and (id='$id' or '$id'=-1) ".$dk1." order by displayorder desc,id desc ".$dk2." LIMIT $startRow , $pageSize";
           	$kq = mysql_query($query) or die(mysql_error());
			return $kq;
		}
		/** get_phantrang_order tương tự get_phantrang_db khác là sắp xếp tùy biến**/
		function get_phantrang_order($pageNum=1,$pageSize=9,&$totalRows,$table,$id=-1,$status=-1,$dk1='') {
			settype($id, "int");
			settype($anhien, "int");
			settype($pageNum, "int");
			if($pageNum<=0) $pageNum=1;
			 $totalRows = 0;
			$query = "SELECT count(id) FROM ".$table." WHERE (status='$status' or '$status'=-1) and (id='$id' or '$id'=-1) ".$dk1;
			//echo $query;
			$kq = mysql_query($query) or die (mysql_error());
            $row_kq = mysql_fetch_row($kq);
            $totalRows = $row_kq[0];

            $startRow = ($pageNum - 1)*$pageSize;
			
			$query = "SELECT * FROM ".$table." WHERE (status='$status' or '$status'=-1) and (id='$id' or '$id'=-1) ".$dk1." LIMIT $startRow , $pageSize";
			//echo $query;
           	$kq = mysql_query($query) or die(mysql_error());
			return $kq;
		}
		
			/** pagelist hiện phân trang trong admin**/
           function pagesList($totalRows , $pageNum=1, $pageSize = 5, $offset = 1){
			$baseURL = $_SERVER['PHP_SELF'];
            parse_str($_SERVER['QUERY_STRING'],$arr);
            unset($arr['pageNum']);
            foreach($arr as $k=> $v) $str.= "&{$k}={$v}";
            $baseURL .="?".$str;

            if ($totalRows<=0) return "";
            $totalPages = ceil($totalRows/$pageSize);
            if ($totalPages<=1) return "";

            $prevLink="";$nextLink="";

            if ($pageNum > 1) {
			$prevPage = $pageNum - 1;
			$prevLink="<span class='showpage'><a href='$baseURL&pageNum=$prevPage'>Trang trước</a></span> &nbsp; |&nbsp;";
            }
            if ($pageNum < $totalPages) {
			$nextPage = $pageNum + 1;
			$nextLink = " &nbsp; |&nbsp; <span class='showpage'><a href='$baseURL&pageNum=$nextPage'>Trang kế</a></span> ";
            }

            $from = $pageNum - $offset;
            $to = $pageNum + $offset;
            if ($from <=0) { $from = 1;   $to = $offset*2; }
            if ($to > $totalPages) { $to = $totalPages; }
            $links = "";
            for($j = $from; $j <= $to; $j++) {
                    if ($j==$pageNum) $links= $links . "<span class='showpagePoint'>$j</span> ";
                    else $links= $links . " <span class='showpageNum'><a href = '$baseURL&pageNum=$j'>$j</a></span> ";
            } //for
			$page_select = '&nbsp;&nbsp;<select id="change_page_select" onchange="root_page(this.value)">';
			for($i=0;$i<$totalPages ; $i++){
					if($pageNum==$i+1)
						$page_select = $page_select.'<option value="'.$baseURL.'&pageNum='.($i+1).$query.'" selected>'.($i+1).'</option>';
					else
						$page_select = $page_select.'<option value="'.$baseURL.'&pageNum='.($i+1).$query.'">'.($i+1).'</option>';
			}
			$page_select = $page_select.'</select>';
			
            return $prevLink.$links.$nextLink.$page_select;
            } 
			/** pageList1 Hiện phan trang ở ngoài có rewrite $baseURL là đường đẫn dẫn**/	
            function pagesList1($baseURL,$totalRows , $pageNum=1, $pageSize = 5, $offset = 5){

            if ($totalRows<=0) return "";
            $totalPages = ceil($totalRows/$pageSize);
            if ($totalPages<=1) return "";

            $prevLink="";$nextLink="";

            if ($pageNum > 1) {
			$prevPage = $pageNum - 1;
			$prevLink="<span class='showpage'><a href='$baseURL/&pageNum=$prevPage.html'>Previous</a></span>";
            }
            if ($pageNum < $totalPages) {
			$nextPage = $pageNum + 1;
			$nextLink = "<span class='showpage'><a href='$baseURL/&pageNum=$nextPage.html'>Next</a></span>";
            }

            $from = $pageNum - $offset;
            $to = $pageNum + $offset;
            if ($from <=0) { $from = 1;   $to = $offset*2; }
            if ($to > $totalPages) { $to = $totalPages; }
            $links = "";
            for($j = $from; $j <= $to; $j++) {
                    if ($j==$pageNum) $links= $links . "<span class='showpagePoint'>$j</span>";
                    else $links= $links . "<span class='showpageNum'><a href = '$baseURL/&pageNum=$j.html'>$j</a></span>";
            } //for
            return $prevLink.$links.$nextLink;
            } // function pagesList
			
			/** pageList3 Hiện phan trang ở ngoài ajax**/	
			function pagesList2($totalRows , $pageNum=1, $pageSize = 5, $offset = 5){
			$baseURL = $_SERVER['PHP_SELF'];
            parse_str($_SERVER['QUERY_STRING'],$arr);
            unset($arr['pageNum']);
            foreach($arr as $k=> $v) $str.= "&{$k}={$v}";
            $baseURL .="?".$str;
			$chuoi=explode("?",$baseURL);
			$baseURL=$chuoi[1];
			$baseURL = substr($baseURL,1);

            if ($totalRows<=0) return "";
            $totalPages = ceil($totalRows/$pageSize);
            if ($totalPages<=1) return "";

            $prevLink="";$nextLink="";

            if ($pageNum > 1) {
			$prevPage = $pageNum - 1;
			$prevLink="<span class='showpage'><a href='javascript:void(0);' url='$baseURL&pageNum=$prevPage'>Trang trước</a></span>";
            }
            if ($pageNum < $totalPages) {
			$nextPage = $pageNum + 1;
			$nextLink = "<span class='showpage'><a href='javascript:void(0);' url='$baseURL&pageNum=$nextPage'>Trang kế</a></span>";
            }

            $from = $pageNum - $offset;
            $to = $pageNum + $offset;
            if ($from <=0) { $from = 1;   $to = $offset*2; }
            if ($to > $totalPages) { $to = $totalPages; }
            $links = "";
            for($j = $from; $j <= $to; $j++) {
                    if ($j==$pageNum) $links= $links . "<span class='showpagePoint'>$j</span>";
                    else $links= $links . "<span class='showpageNum'><a href = 'javascript:void(0);' url='$baseURL&pageNum=$j' >$j</a></span>";
            } //for
            return $prevLink.$links.$nextLink;
            } // function pagesList
			
            
}
?>